package cn.doitedu.cn.doitedu.rtdw.udfs;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.flink.table.functions.ScalarFunction;

import java.text.ParseException;

public class TimestampTrunc extends ScalarFunction {

    public  String  eval(String timestamp,int intervalMinutes) throws ParseException {

        long time = DateUtils.parseDate(timestamp, "yyyy-MM-dd HH:mm:ss.SSS").getTime();
        long interval = intervalMinutes*60*1000;
        long trunked = (time/interval)*interval;

        return DateFormatUtils.format(trunked,"yyyy-MM-dd HH:mm:ss");
    }
}
